package com.boot.mall.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.mall.product.entity.AttrEntity;
import com.boot.mall.product.entity.CategoryEntity;
import com.boot.mall.product.service.AttrAttrgroupRelationService;
import com.boot.mall.product.service.CategoryService;
import com.boot.mall.product.vo.AttrGroupWithAttrsVo;
import com.boot.mall.product.vo.SkuItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boot.common.utils.PageUtils;

import com.boot.mall.product.dao.AttrGroupDao;
import com.boot.mall.product.entity.AttrGroupEntity;
import com.boot.mall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    // 分页查询
    @Override
    public PageUtils pageQuery(Map<String, Object> params, Long categoryId) {
        String curr = (String) params.get("page");
        long current = 1;
        if (curr != null) {
            current = Integer.parseInt(curr);
        }
        long limit = 1;
        String limitStr = (String) params.get("limit");
        if (limitStr != null) {
            limit = Integer.parseInt(limitStr);
        }
        // 三级分类id为0，查询全部
        if (categoryId == 0){
            // 创建分页对象
            Page<AttrGroupEntity> page = new Page<>(current,limit);
            // 分页查询所有
            baseMapper.selectPage(page,null);
            PageUtils pageUtils = new PageUtils(page.getRecords(),(int)page.getTotal(),params.size(),(int)current);
            return pageUtils;
        }else {
            // 设置三级分类id
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id",categoryId);
            String key = params.get("key").toString();
            if (!StringUtils.isEmpty(key)) {
                // 条件拼接
                wrapper.and((obj)->{
                    obj.eq("attr_group_id",key).or().like("attr_group_name",key);
                });
            }
            Page<AttrGroupEntity> page = new Page<>(current,limit);
            // 分页查询所有
            baseMapper.selectPage(page,wrapper);
            PageUtils pageUtils = new PageUtils(page.getRecords(),(int)page.getTotal(),params.size(),(int)current);
            return pageUtils;
        }
    }

    // 查询分组分类的完整信息
    @Override
    public AttrGroupEntity getAttrGroupById(String attrGroupId) {
        // 查询当前分组的信息
        AttrGroupEntity attr = baseMapper.selectById(attrGroupId);
        // 根据attr的分类id查询完整的分类路径id
        // 查询三级分类的信息，其中三级分类包括三级分类的id和二级分类的id
        QueryWrapper<CategoryEntity> oneWrapper = new QueryWrapper<>();
        oneWrapper.eq("cat_id",attr.getCatelogId());
        // 获取二级分类id
        CategoryEntity one = categoryService.getOne(oneWrapper);
        // 二级分类id
        String twoPid = one.getParentCid();
        // 获取一级分类id
        QueryWrapper<CategoryEntity> twoWrapper = new QueryWrapper<>();
        twoWrapper.eq("cat_id",twoPid);
        CategoryEntity two = categoryService.getOne(twoWrapper);
        // 一级分类id
        String onePid = two.getParentCid();
        // 设置路径
        String[] ids = {onePid,twoPid,attr.getCatelogId()};
        attr.setCatelogPath(ids);
        return attr;
    }

    // 根据分类id查询分组属性信息
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCatId(String categoryId) {
        List<AttrGroupWithAttrsVo> list = new ArrayList<>();
        // 根据三级分类的id查询分组信息集合
        List<AttrGroupEntity> attrGroups = baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", categoryId));
        // 遍历分组集合
        for (AttrGroupEntity attrGroup : attrGroups) {
            // 根据分组的id查询分组的详细属性信息
            List<AttrEntity> attrs = attrAttrgroupRelationService.getRelationAttrByGroupId(attrGroup.getAttrGroupId());
            // 给属性赋值
            AttrGroupWithAttrsVo vo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(attrGroup,vo);
            vo.setAttrs(attrs);
            list.add(vo);
        }
        return list;
    }

    @Override
    public List<SkuItemVo.SpuItemGroupAttr> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        // 1.查询出当前spu对应的所有分组信息以及分组对应的值
        List<SkuItemVo.SpuItemGroupAttr> groupAttrs = baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);
        return groupAttrs;
    }
}